package web.fabrimind.bdm.service.impl;

import java.util.List;
import web.fabrimind.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import web.fabrimind.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import web.fabrimind.bdm.domain.ProductionLine;
import web.fabrimind.bdm.mapper.WorkshopMapper;
import web.fabrimind.bdm.domain.Workshop;
import web.fabrimind.bdm.service.IWorkshopService;

/**
 * 车间管理Service业务层处理
 * 
 * @author 智慧
 * @date 2025-03-27
 */
@Service
public class WorkshopServiceImpl implements IWorkshopService 
{
    @Autowired
    private WorkshopMapper workshopMapper;

    /**
     * 查询车间管理
     * 
     * @param workshopId 车间管理主键
     * @return 车间管理
     */
    @Override
    public Workshop selectWorkshopByWorkshopId(Long workshopId)
    {
        return workshopMapper.selectWorkshopByWorkshopId(workshopId);
    }

    /**
     * 查询车间管理列表
     * 
     * @param workshop 车间管理
     * @return 车间管理
     */
    @Override
    public List<Workshop> selectWorkshopList(Workshop workshop)
    {
        return workshopMapper.selectWorkshopList(workshop);
    }

    /**
     * 新增车间管理
     * 
     * @param workshop 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkshop(Workshop workshop)
    {
        workshop.setCreateTime(DateUtils.getNowDate());
        int rows = workshopMapper.insertWorkshop(workshop);
        insertProductionLine(workshop);
        return rows;
    }

    /**
     * 修改车间管理
     * 
     * @param workshop 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkshop(Workshop workshop)
    {
        workshop.setUpdateTime(DateUtils.getNowDate());
        return workshopMapper.updateWorkshop(workshop);
    }

    /**
     * 批量删除车间管理
     * 
     * @param workshopIds 需要删除的车间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkshopByWorkshopIds(Long[] workshopIds)
    {
        workshopMapper.deleteProductionLineByWorkshopIds(workshopIds);
        return workshopMapper.deleteWorkshopByWorkshopIds(workshopIds);
    }

    /**
     * 删除车间管理信息
     * 
     * @param workshopId 车间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkshopByWorkshopId(Long workshopId)
    {
        workshopMapper.deleteProductionLineByWorkshopId(workshopId);
        return workshopMapper.deleteWorkshopByWorkshopId(workshopId);
    }

    /**
     * 新增生产线管理信息
     * 
     * @param workshop 车间管理对象
     */
    public void insertProductionLine(Workshop workshop)
    {
        List<ProductionLine> productionLineList = workshop.getProductionLineList();
        Long workshopId = workshop.getWorkshopId();
        if (StringUtils.isNotNull(productionLineList))
        {
            List<ProductionLine> list = new ArrayList<ProductionLine>();
            for (ProductionLine productionLine : productionLineList)
            {
                productionLine.setWorkshopId(workshopId);
                list.add(productionLine);
            }
            if (list.size() > 0)
            {
                workshopMapper.batchProductionLine(list);
            }
        }
    }

    /**
     * 更新生产线管理信息，不删除现有生产线，只进行添加操作
     * 
     * @param workshop 车间管理对象
     */
    public void updateProductionLine(Workshop workshop)
    {
        List<ProductionLine> productionLineList = workshop.getProductionLineList();
        Long workshopId = workshop.getWorkshopId();
        if (StringUtils.isNotNull(productionLineList))
        {
            List<ProductionLine> list = new ArrayList<ProductionLine>();
            for (ProductionLine productionLine : productionLineList)
            {
                // 只处理新增的生产线（没有ID的记录）
                if (productionLine.getLineId() == null)
                {
                    productionLine.setWorkshopId(workshopId);
                    list.add(productionLine);
                }
            }
            if (list.size() > 0)
            {
                workshopMapper.batchProductionLine(list);
            }
        }
    }
}
